﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace PuroGame.Helpers
{
    public class ImageHelper
    {
        //GerarImagem(url, 720, 0, ImageFormat.Jpeg, true, codSequencia, SAVE_PATH + codSequencia.ToString() + "_ssx.jpg");
        
        #region GerarImagem
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objImage"></param>
        /// <param name="iAltura"></param>
        /// <param name="iLargura"></param>
        /// <param name="imageFormat"></param>
        /// <param name="bManterProporcionalidade"></param>
        /// <param name="sequencia"></param>
        /// <param name="filename"></param>
        public static void GerarImagem(Image objImage, int iAltura, int iLargura, ImageFormat imageFormat, string filename)
        {
            Image newImage = null;
            float alturaPerc, larguraPerc;

            iLargura = objImage.Width;
            if (iAltura.Equals(0))
                iAltura = objImage.Height;

            if (objImage != null)
            {
                if (iAltura > iLargura)
                {
                    larguraPerc = (float)iLargura / objImage.Width;
                    iAltura = Convert.ToInt32(objImage.Height * larguraPerc);
                }
                else
                {
                    alturaPerc = (float)iAltura / objImage.Height;
                    iLargura = Convert.ToInt32(objImage.Width * alturaPerc);
                }

                /*
                 * Porcentagem de qualidade da imagem 
                 */
                Int64 param = 75L;
                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                Encoder objEncoder = Encoder.Quality;

                EncoderParameters objEncoderParams = new EncoderParameters(1);
                EncoderParameter objEncoderParam = new EncoderParameter(objEncoder, param);
                objEncoderParams.Param[0] = objEncoderParam;

                newImage = new Bitmap(iLargura, iAltura, objImage.PixelFormat);

                Graphics objGraphics = Graphics.FromImage(newImage);
                objGraphics.CompositingQuality = CompositingQuality.HighSpeed;
                objGraphics.SmoothingMode = SmoothingMode.HighSpeed;
                objGraphics.InterpolationMode = InterpolationMode.Default;

                if (imageFormat.Equals(ImageFormat.Png))
                    objGraphics.Clear(Color.Transparent);
                else
                    objGraphics.Clear(Color.White);

                Rectangle objRectangle = new Rectangle(0, 0, iLargura, iAltura);
                objGraphics.DrawImage(objImage, objRectangle);

                newImage.Save(filename, jpgEncoder, objEncoderParams);
                newImage.Dispose();
                objGraphics.Dispose();
            }

            //objImage.Dispose();

        }
        #endregion

        #region GetEncoder
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
        #endregion
    }
}
